﻿#include "QSSLoader.h"
#include <QFileSystemWatcher>
#include <QDebug>
#include <QMessageBox>
#include <QTimer>

QSSLoader &QSSLoader::instance()
{

    static QSSLoader instance;
    return instance;

}

QString QSSLoader::LoadStyleSheet(const QString &filePath)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "QSSLoader: Failed to open file:" << filePath;
        return QString();
    }

    QString styleContent = file.readAll();
    file.close();

    return styleContent;
}

QSSLoader::QSSLoader(QObject *parent)
{
    Q_UNUSED(parent);
}



QSSLoader::~QSSLoader()
{
    teardownFileWatcher();
}

bool QSSLoader::load(const QString &filePath, const QString &key)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "QSSLoader: Failed to open file:" << filePath;
        return false;
    }

    QString styleContent = QLatin1String(file.readAll());
    file.close();

    QString actualKey = key.isEmpty() ? filePath : key;

    m_styleFiles[actualKey] = filePath;
    m_styleSheets[actualKey] = styleContent;

    if (m_watchFiles) {
        setupFileWatcher();
    }

    updateApplicationStyle();
    return true;
}

void QSSLoader::remove(const QString &key)
{
    if (m_styleSheets.remove(key))
    {
        m_styleFiles.remove(key);
        updateApplicationStyle();
    }

    if (m_watchFiles)
    {
        setupFileWatcher();
    }
}

void QSSLoader::clear()
{
    m_styleSheets.clear();
    m_styleFiles.clear();
    qApp->setStyleSheet("");

    if (m_watchFiles) {
        teardownFileWatcher();
    }
}

QString QSSLoader::currentStyle() const
{
    QString result;
    for (const auto& style : m_styleSheets) {
        result += style + "\n";
    }
    return result;
}

void QSSLoader::reloadAll()
{
    auto files = m_styleFiles;
    clear();

    for (auto it = files.begin(); it != files.end(); ++it) {
        load(it.value(), it.key());
    }
}

void QSSLoader::setWatchFiles(bool enable)
{
    if (m_watchFiles == enable) {
        return;
    }

    m_watchFiles = enable;
    if (m_watchFiles) {
        setupFileWatcher();
    } else {
        teardownFileWatcher();
    }
}



void QSSLoader::updateApplicationStyle()
{
    QString combinedStyle;
    for (const auto& style : m_styleSheets) {
        combinedStyle += style + "\n";
    }
    qApp->setStyleSheet(combinedStyle);
}

void QSSLoader::setupFileWatcher()
{
    if (!m_fileWatcher) {
        m_fileWatcher = new QFileSystemWatcher(this);
        connect(m_fileWatcher, &QFileSystemWatcher::fileChanged,
                this, [this](const QString& path) {
            // 文件变化时重新加载
            qDebug() << "QSS file modified:" << path;
            QTimer::singleShot(100, this, [this, path]() {
                // 找到对应的key
                for (auto it = m_styleFiles.begin(); it != m_styleFiles.end(); ++it) {
                    if (it.value() == path) {
                        load(path, it.key());
                        break;
                    }
                }
            });
        });
    }

    // 更新监视的文件列表
    m_fileWatcher->removePaths(m_fileWatcher->files());
    m_fileWatcher->addPaths(m_styleFiles.values());
}

void QSSLoader::teardownFileWatcher()
{
    if (m_fileWatcher) {
        m_fileWatcher->deleteLater();
        m_fileWatcher = nullptr;
    }
}
